<template>
  <div class="app-container">
    <!-- 顶部搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入表单名称或者key"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-divider direction="vertical"></el-divider>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column label="id" align="left" prop="id" width="150"/>
      <el-table-column label="key" align="left" prop="formKey" width="150"/>
      <el-table-column label="版本" align="left" prop="version" width="100"/>
      <el-table-column label="标题" align="left" prop="formTitle" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="说明" align="left" prop="formDesc" :show-overflow-tooltip="true" width="300"/>
      <el-table-column label="创建人" align="left" prop="createBy" width="100"/>
      <el-table-column label="创建时间" align="left" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            :data-clipboard-text="ip + '/openform/review/def/' + scope.row.id"
            class="iconfont icon-copy"
          >复制链接</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleShowForm(scope.row)"
          >预览</el-button>

          <el-dropdown style="margin-left: 10px;">
            <el-button type="text" size="mini">
              更多操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleHistory(scope.row)" icon="el-icon-document-copy">历史版本</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)" divided icon="el-icon-delete">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
    import {listDef, removeDef} from "@/api/openform/generator";
    import Clipboard from 'clipboard';

    export default {
        name: "FormDefList",
        data() {
            return {
                loading: true,// 遮罩层
                ids: [],// 选中数组
                single: true,// 非单个禁用
                multiple: true,// 非多个禁用
                total: 0,// 总条数
                dataList: [],// 参数表格数据
                queryParams: {// 查询参数
                    pageNum: 1,
                    pageSize: 10,
                    keyword: undefined
                },
                ip: null,
                clipboard: null
            };
        },
        created() {//创建回调
            this.getList();//查询数据
        },
        mounted() {
            if(!this.clipboard) {
                this.clipboard = new Clipboard('.icon-copy');
                this.clipboard.on('success', e => {
                    this.$message.info('已复制：'+e.text);
                });
                this.ip = location.origin;
            }
        },
        methods: {
            // 查询数据
            getList() {
                this.loading = true;
                listDef(this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.dialog.open = false;
                this.clearForm();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!=1
                this.multiple = !selection.length
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 跳转页面 */
            handleShowForm(row) {
                this.$router.push({ path: "/openform/review/def/"+(row.id || this.ids[0]) });
            },
            handleHistory(row) {
                this.$router.push({ path: "/openform/hisList/"+row.formKey });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const dataId = row.id || this.ids;
                this.$confirm('表定义删除将同时删除数据，并且不可恢复，是否继续?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return removeDef(dataId);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
        }
    };
</script>
<style>
</style>
